<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />

</head>
<body class="childrenBody">

<style type="text/css">

    .layui-table-cell {
        height: auto;
        line-height: 28px;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
</style>

<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户手机</label>
                <div class="layui-input-inline">
                    <input type="text" name="userPhone"  autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">订单号</label>
                <div class="layui-input-inline">
                    <input type="text" name="id"  autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">支付状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="orderState" value="0" title="未支付">
                    <input type="radio" name="orderState" value="1" title="已支付">
                    <input type="radio" name="orderState" value="2" title="已取消" >
                    <input type="radio" name="orderState" value="3" title="支付超时">
                    <input type="radio" name="orderState" value="4" title="所有" checked="">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="startTime" id="startTime" readonly="readonly" placeholder="yyyy-MM-dd HH:mm:ss"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="endTime"  id="endTime" readonly="readonly" placeholder="yyyy-MM-dd HH:mm:ss"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>

    </form>
</blockquote>
<blockquote class="layui-elem-quote">
    <div>
        <label>订单总金额:</label>
        <label id="amount">0.00</label>
    </div>
</blockquote>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="orderTable" lay-filter="orderTable"></table>

    <div id="roleRowBar" style="display: none;">

        <button type="button" lay-event="showOrderInfo" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-picture"></span>详情</button>
        <button type="button" lay-event="confirmPay" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-picture"></span>确认支付</button>
        <button type="button" lay-event="cancelPay" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-picture"></span>取消支付</button>

    </div>

    <div id="orderInfoBar" style="display: none;visible:hidden">


    </div>
</div>


<!-- 数据表格结束 -->


<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="orderInfoDiv">
    <div>
        <table class="layui-hide" id="orderInfoTable" lay-filter="orderInfoTable"></table>
    </div>
</div>
        <!-- 添加和修改的弹出层结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery','form','table','layer','dtree','laydate'],function(){
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate = layui.laydate;

        var curtime = getyyyyMMdd() + " 00:00:00";
        var curtimeend = getyyyyMMdd() + " 23:59:59";

       // console.log(curtime);
        laydate.render({
            elem:'#startTime',
            type:'datetime',
            value:curtime,

        })
        laydate.render({
            elem:'#endTime',
            type:'datetime',
            value:curtimeend,
        })

        //加载 数据
        var tableIns=table.render({
            elem: '#orderTable'
            ,url:'/order/getUserOrderList'
            ,toolbar: '#roleToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '订单表'
            ,height:'full-180'
            ,page: true
            ,limit:50
            ,limits:[50,100,150]
            ,cols: [ [
                {field:'orderId', title:'订单号',align:'center',width:'200'}
                ,{field:'userPhone', title:'下单账号',align:'center',width:'150'}
                ,{field:'userName', title:'用户名称',align:'center',width:'150'}
                ,{field:'orderAmount', title:'订单总价',align:'center',width:'80'}
                ,{field:'orderState', title:'订单状态',align:'center',width:'100',templet:function(d){

                        let state = ["未支付","已支付","已取消","支付超时"][d.orderState];
                        return '<font color=green>' + state + '</font>';
                    }}
                ,{field:'createTime', title:'生成时间',align:'center',width:'150'}
                ,{field:'deliveryType', title:'商品种数',align:'center',width:'80',templet:function(d){

                    let num = JSON.parse(d.orderDetail).length;
                             return '<font color=green>' + num + '</font>';
                    }}

                ,{field:'deliveryType', title:'配送方式',align:'center',width:'80',templet:function(d){
                        if(d.deliveryType==1)
                            return '<font color=green>自提</font>';
                        else
                            return '<font color=red>送货上门</font>';}}
                ,{fixed: 'right', title:'操作', toolbar: '#roleRowBar',align:'center',width:'200'}
            ] ]
            ,done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度

                console.log(res);
                $("#amount").text(res.otherMsg);
                //得到当前页码
                console.log(curr);

                //得到数据总量
                console.log(count);
            }
        });


        var infotableIns=table.render({
            elem: '#orderInfoTable'
            // ,url:'/orderInfo/getOrderList'
            ,toolbar: '#orderInfoBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '客户数据表'
            ,height:'full-110'
            ,page: true
            ,data:[]
            ,cols: [ [
                {field:'goodsCount', title:'缩略图',align:'center',width:'200',templet:function (d) {

                    return '<div><img src=' + '/resources/images/' +  d.goodsImg +'></div>'}}

                ,{field:'goodsName', title:'商品名称',align:'center',width:'80'}
                ,{field:'goodsCount', title:'数量',align:'center',width:'70'}
                ,{field:'sellPrice', title:'售价',align:'center',width:'80'}
                ,{field:'sellPrice', title:'总价',align:'center',width:'80',templet:function(d){
                        let totalMoney = d.sellPrice * d.goodsCount;

                        return '<font color=red>' + totalMoney + '</font>';

                    }}

                ,{field:'salerName', title:'供货商',align:'center',width:'80'}
                ,{field:'salerPrice', title:'供货商价格',align:'center',width:'80'}
                ,{field:'diffMoney', title:'差价',align:'center',width:'80'}

                ,{field:'state', title:'商品状态',align:'center',width:'80',templet:function(d){
                    let goodsState = ["备货正常","缺货","已退货"][d.state];
                    if(d.state == 0)
                     return '<font color=green>' + goodsState + '</font>';
                    else
                        return '<font color=red>' + goodsState + '</font>';

                    }}

                ,{field: 'right', title:'操作', toolbar: '#orderInfoBar',align:'center',width:'100'}
            ] ]
            ,done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度

                console.log(res);
                $("#amount").text(res.otherMsg);
                //得到当前页码
                console.log(curr);

                //得到数据总量
                console.log(count);
            }
        });




        //模糊查询
        form.on("submit(doSearch)",function(data){
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        $("#doReset").click(function () {

            $("#dataFrm")[0].reset();

        })
        //监听工具条的事件
        table.on("toolbar(orderTable)",function(obj){
            switch(obj.event){
                case 'add':
                    openAddLayer();
                    break;
            };
        });

        //监听行工具条的事件
        table.on("tool(orderTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'update':
                    openUpdateRoleLayer(data);
                    break;
                case 'showOrderInfo':
                    showOrderInfo(data);
                    break;
                case 'confirmPay':
                    confirmPay(data);
                    break;
                case 'cancelPay':
                    cancelPay(data);
                    break;

            };
        });

        var mainIndex;
        var url;

        //打开修改的弹出层
        function openUpdateRoleLayer(data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','600px'],
                title:'修改角色',
                success:function(){

                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url="/role/updateRole";
                }
            });
        }

        form.on("submit(doSubmit)",function(data) {
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        });
        form.on("submit(doCancel)",function(data) {

            layer.close(mainIndex);
            return false;
        });


        //确认支付
        function confirmPay(data)
        {
            layer.confirm('确认该订单支付了吗?', {icon: 3, title:'提示'}, function(index){
                //data.esStatus = 1;
                $.post("/order/confirmPay",{"orderId":data.id,"state":1},function(res){
                    if(res.code==200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });

        }
        function cancelPay(data)
        {
            layer.confirm('确认取消支付吗?', {icon: 3, title:'提示'}, function(index){
                //data.esStatus = 1;
                $.post("/order/confirmPay",{"orderId":data.id,"state":0},function(res){
                    if(res.code==200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });

        }

        //订单详情
        function showOrderInfo(data) {

            mainIndex=layer.open({
                type:1,
                content:$("#orderInfoDiv"),
                area:['1520px','820px'],
                title:'查看订单详情',
                success:function(){
                    $.post("/order/getUserOrderDetail",{"orderId":data.id,"state":0},function(res){
                        if(res.code==0){
                            infotableIns.reload({
                                data: res.data
                            })                        }
                        else
                            layer.msg(res.msg);
                    })

                        

            }
            })
        }

        function getyyyyMMdd(){
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1;
            var curr_year = d.getFullYear();
            String(curr_month).length < 2 ? (curr_month = "0" + curr_month): curr_month;
            String(curr_date).length < 2 ? (curr_date = "0" + curr_date): curr_date;
            var yyyyMMdd = curr_year + "-" + curr_month +"-"+ curr_date;
            return yyyyMMdd;
        }
    });
</script>

</body>
</html>